export default class BaseSprite extends godot.Sprite {
    x = 200
    y = 200
    xv = 3
    yv = 3
    face : 0|1|2|3|4 = 0  // 0 1 2 3 4 不变 上,下,左,右

    isAutoMove = false
    isControlMove = false
    isInputMove = false


    _process(delta: number): void {
        if (this.isAutoMove) {
            if (this.x < 800) {
                this.x = this.x + this.xv
            } else {
                this.x = 200
            }
            this.position = new godot.Vector2(this.x, this.y);
        }
        if(this.isInputMove){
            this.handleInput()
        }
    }

    _input(event: godot.InputEvent): void {
        // is_pressed() 按下触发 否则触发2次,按下与释放都会触发
        if (this.isControlMove && event.is_pressed()) {
            this.handEvent(event);
        }
    }

    handleInput(){
        this.face = 0;
        
        if (godot.Input.is_key_pressed(godot.KEY_D)) {
            this.face = 4
            this.x = this.x + this.xv
        } else if (godot.Input.is_key_pressed(godot.KEY_A)) {
            this.face = 3
            this.x = this.x - this.xv
        } else if (godot.Input.is_key_pressed(godot.KEY_W)) {
            this.face = 1
            this.y = this.y - this.yv
        } else if (godot.Input.is_key_pressed(godot.KEY_S)) {
            this.face = 2
            this.y = this.y + this.yv
        } else if( godot.Input.is_mouse_button_pressed(godot.BUTTON_LEFT)){
            let pos = this.get_viewport().get_mouse_position();
            this.x = pos.x;
            this.y = pos.y;
        }
        this.position = new godot.Vector2(this.x, this.y)
    }

    handEvent(event: godot.InputEvent): void {
        if (event instanceof godot.InputEventKey) {
            if (event.scancode == godot.KEY_D) {
                this.x = this.x + this.xv
            } else if (event.scancode == godot.KEY_A) {
                this.x = this.x - this.xv
            } else if (event.scancode == godot.KEY_W) {
                this.y = this.y - this.yv
            } else if (event.scancode == godot.KEY_S) {
                this.y = this.y + this.yv
            }
        } else if (event instanceof godot.InputEventMouseButton) {
            if (event.button_index == godot.BUTTON_LEFT) {
                this.x = event.position.x;
                this.y = event.position.y
            }
        }
        this.position = new godot.Vector2(this.x, this.y)
    }
}